<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.io.DataInputStream"%>
<%@page import="java.io.FileOutputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="database" scope="request" class="accessibleworldsdatabase.AccessibleWorldsDatabaseJavaBeans"/>
<jsp:useBean id="des" scope="request" class="accessibleworldsdes.AccessibleWorldsDESJavaBeans"/>
<%try {
	
	DataInputStream inputStream = new DataInputStream(request.getInputStream());
	int dataLength = request.getContentLength();
	
	byte [] dataBytes = new byte[dataLength];
	int readBytes = 0;
	int totalReadBytes = 0;
	while (totalReadBytes < dataLength) {
	    readBytes = inputStream.read(dataBytes, totalReadBytes, dataLength);
	    totalReadBytes += readBytes;
	}
	
	int accessCodeStart = findChar('=', dataBytes , 0 ,dataLength)+1;
	int accessCodeLength = findChar('&', dataBytes ,accessCodeStart,dataLength) - accessCodeStart;
	String accessCode = new String(dataBytes,accessCodeStart,accessCodeLength);
	
	int reportIDStart = findChar('=', dataBytes , accessCodeStart+accessCodeLength ,dataLength)+1;
	int reportIDLength = findChar('&', dataBytes ,reportIDStart,dataLength) - reportIDStart;
	String reportID = new String(dataBytes,reportIDStart,reportIDLength);
	
	int userIDStart = findChar('=', dataBytes , reportIDStart+reportIDLength ,dataLength)+1;
	int userIDLength = findChar('&', dataBytes ,userIDStart,dataLength) - userIDStart;
	String userID = new String(dataBytes,userIDStart,userIDLength);
	
	int dataStart = findChar('=', dataBytes , userIDStart+userIDLength,dataLength)+1;
	
	if(!(accessCode==null || "".equals(accessCode) || reportID==null || "".equals(reportID) || userID==null || "".equals(userID))) {
	    
	    accessCode = URLDecoder.decode(accessCode, "UTF-8");
	    accessCode = des.decryptString(des.getServiceSecretKey(), accessCode);
	    accessCode = URLDecoder.decode(accessCode, "UTF-8");
	    
	    if(des.getAndroidAccessCode().equals(accessCode) || des.getComputerAccessCode().equals(accessCode)) {
	        
	        reportID = URLDecoder.decode(reportID, "UTF-8");
	        reportID = des.decryptString(des.getServiceSecretKey(), reportID);
	        reportID = URLDecoder.decode(reportID, "UTF-8");
	        userID = URLDecoder.decode(userID, "UTF-8");
	        userID = des.decryptString(des.getServiceSecretKey(), userID);
	        userID = URLDecoder.decode(userID, "UTF-8");
	        
	        database.connect(des.decryptString(des.getServiceSecretKey(), database.getEncryptedPassword()));
	
	        String SQL =
	        "INSERT INTO Videos"+
	        	" (Report_id"+
	        	" ,User_id)"+
	        	" VALUES"+
	        		" ("+reportID+""+
	        		" ,"+userID+")";
	        
	        ResultSet resultSet = database.updateSQLWithGeneratedKeys(SQL);
	        String videoID = "";
	        if(resultSet.next()) {
	            videoID = ""+resultSet.getInt(1);
	        }
	            
	        String saveFile = getServletContext().getInitParameter("mediaLocation")+videoID+".3gp";
	        FileOutputStream fileOut = new FileOutputStream(saveFile);
	        fileOut.write(dataBytes, dataStart, dataLength-dataStart);
	        fileOut.flush();
	        fileOut.close();
	          
	            
	        String output = URLEncoder.encode("OK","UTF-8");
	        output = des.encryptString(des.getServiceSecretKey(),output);
	        out.print(output);
	        
	        database.closeConnection();
	    }
	}
} catch (Exception ex) {}%>
<%!
public int findChar (char c ,byte [] dB, int start , int size) {
    for (int i = start ; i<size ; i++) {
        if(c == (char)dB[i]) {
            return i;
        }
    }
    return -1;
}
%>